package org.realogic.btp.web.taglib;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.realogic.btp.dhtmlx.define.DhtmlxGridField;
import org.realogic.btp.dhtmlx.define.DhtmlxGridModel;
import org.realogic.btp.dhtmlx.manager.DhtmlxGridManager;
import org.realogic.cfm.KAppContext;
import org.realogic.cfm.authenticate.KSessionUser;

public class DhtmlxGridStatTag extends TagSupport {

	private static final long serialVersionUID = 6336653084152003151L;
	
	private String name = null;
	private String varGrid = null;

	public void setName(String name) {
		this.name = name;
	}

	public void setVarGrid(String varGrid) {
		this.varGrid = varGrid;
	}

	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			DhtmlxGridManager tmpDhtmlxgridMgr = DhtmlxGridManager.getMe();
			DhtmlxGridModel tmpModel = tmpDhtmlxgridMgr.getDhtmlxGridModel(name);
			KSessionUser usom = KAppContext.currentUser((HttpServletRequest) pageContext.getRequest());
			if (tmpModel != null) {
				DhtmlxGridField tmpDhtmlxGridField = null;
				List<DhtmlxGridField> items = tmpModel.getItems();
				int size = items.size();
				for (int i = 0; i < size; i++) {
					tmpDhtmlxGridField = items.get(i);
					if ("#cspan".equals(tmpDhtmlxGridField.getFooter()) || "".equals(tmpDhtmlxGridField.getFooter())) {
						continue;
					}
					if (!tmpDhtmlxGridField.getAuth().equals("") && !usom.hasPermission(tmpDhtmlxGridField.getAuth())) {
						continue;
					}
					out.print("document.getElementById(\"" + tmpDhtmlxGridField.getFooter() + "\").innerHTML=" + varGrid + ".getUserData(\"\",\"" + tmpDhtmlxGridField.getFooter() + "\")");
					out.print("\n");
				}
			} else {
				throw new JspException(name + "不存在....");
			}
		} catch (Throwable e) {
			throw new JspException(e);
		}
		return EVAL_PAGE;
	}
}